<?php

class MHangsanxuat extends CI_Model {

    private $mahangsx;
    private $tenhangsx;

    public function __construct() {
        parent::__construct();
        $this->load->database();
        $this->OnCreate();
    }

    function OnCreate() {
        $this->mahangsx = '';
        $this->tenhangsx = '';
    }

    public function set_mahangsx($data) {
        $this->mahangsx = $data;
    }

    public function set_tenhangsx($data) {
        $this->tenhangsx = $data;
    }

    public function get_mahangsx() {
        return $this->mahangsx();
    }

    public function get_tenhangsx() {
        return $this->tenhangsx;
    }

//////////////////////////
    public function Save() {
        $data = array("mahangsx" => $this->mahangsx, "tenhangsx" => $this->tenhangsx);
        $this->db->insert("hangsanxuat", $data);
    }

    public function Insert($mahangsx, $tenhangsx) {
        $data = array("mahangsx" => $mahangsx, "tenhangsx" => $tenhangsx);
        return $this->db->insert("hangsanxuat", $data);
    }

    public function UpdateField($strWhere = NULL) {
        $arr = array();
        $arr["mahangsx"] = $this->mahangsx;
        $arr["tenhangsx"] = $this->tenhangsx;
        if (!empty($strWhere)) {
            $this->db->where($strWhere, NULL, FALSE);
        }
        $this->db->update("hangsanxuat", $arr);
    }

    public function Delete($strWhere = NULL) {
        if (!is_null($strWhere)) {
            $this->db->where($strWhere, NULL, FALSE);
            $this->db->delete("hangsanxuat");
        }
        else
            $this->db->empty_table('hangsanxuat');
    }

    public function get_top_id() {
        $Query = $this->db->query("select * from hangsanxuat");
        $dem = $Query->num_rows();
        return $dem + 1;
    }

    public function getAllhangsx() {
        return $this->db->get('hangsanxuat')->result_array();
    }

    public function SearchByIDorName($keyword) {
        return $this->db->like('mahangsx', $keyword)->or_like('tenhangsx', $keyword)->get('hangsanxuat')->result_array();
    }

    public function DeleteByID($id) {
        // $this->db->trans_start();
        return $this->db->where('mahangsx', $id)->delete('hangsanxuat');

//        if ($this->db->trans_status() === FALSE) {
//            $this->db->trans_rollback();
//            return FALSE;
//        }
//        $this->db->trans_complete();
//        return TRUE;
//  $this->db->af
    }

    public function getByID($mahangsx) {
        return $this->db->get_where('hangsanxuat', array('mahangsx' => $mahangsx))->row_array();
    }

    public function UpdateByID() {
        $arr = array();
        $arr["mahangsx"] = $this->mahangsx;
        $arr["tenhangsx"] = $this->tenhangsx;
        $this->db->where('mahangsx', $this->mahangsx);
        return $this->db->update("hangsanxuat", $arr);
    }

}

?>
